package com.lxs.mall.order.interceptor;

import com.lxs.common.constant.AuthConstant;
import com.lxs.common.vo.MemberVO;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AuthInterceptor implements HandlerInterceptor {

    public static  ThreadLocal threadLocal=new ThreadLocal();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        String requestURI =request.getRequestURI();
        boolean match = new AntPathMatcher().match("/orderPay/**", requestURI);
        if(match){
            return true;
        }

        //通过HttpSession获取当前登录用户的信息
        HttpSession session = request.getSession();
        Object attribute = session.getAttribute(AuthConstant.AUTH_SESSION_REDIS);
        if(attribute!=null){
            MemberVO memberVO= (MemberVO) attribute;
            threadLocal.set(memberVO);
            return true;
        }
        //如果session=null说明没有登录
        session.setAttribute(AuthConstant.AUTH_SESSION_MSG,"请先登录");
        response.sendRedirect("http://auth.lxsde.com/login.html");
        return false;
    }
}
